package com.facebook.games.fa.model
{
	
	/**
	 * Round
	 * @author Diego Maehler <dmaehler@gmail.com>
	 */
	[Bindable]
	public class Round
	{
		
		public var id : int;
		
		/**
		 * Número do round
		 */
		public var number : int;
		
		/**
		 * Jogo
		 */
		public var game : Game;
		
		/**
		 * Jogador vencedor do round
		 */
		public var winner : Player;
		
		/**
		 * Determina se o round já foi finalizado ou não
		 */
		private var _isFinalized : Boolean = false;
		
		/**
		 * Carta do jogador um
		 */
		public var cardOne : Card;
		
		/**
		 * Carta do jogador dois
		 */
		public var cardTwo : Card;
		
		/**
		 * Atributo que será comparado no round
		 */
		public var attributeName : String;
		
		public function get isFinalized () : Boolean
		{
			return this._isFinalized;
		}
		
		/**
		 * 
		 */
		public function Round (game : Game = null, number : int = 0)
		{
			this.game = game;
			this.number = number;
		}
		
		/**
		 * Determina o jogador vencendor
		 * @param Nome do atributo a ser comparador
		 * @param Finalizar o round?
		 * @return Player vencedor
		 */
		public function determineWinner (attributeName : String, finalize : Boolean = true) : Player
		{
			const result : int = this.cardOne.compareAthletesAttribute(this.cardTwo, attributeName);
			
			switch (result) {
				case -1	: winner = this.game.playerTwo; break;
				case 0	: winner = this.determineDrawWinner();
				case 1	: winner = this.game.playerOne; break;
			}
			
			if (winner != null) {
				winner.aditionalPack.addCard(this.cardOne);
				winner.aditionalPack.addCard(this.cardTwo);
				this.getCardsDrawed();
			} else {
				this.putCardsDrawed();
			}
			
			this._isFinalized = finalize;
		}
		
		/**
		 * Determina um vencedor, caso haja um empate.
		 * @return winner
		 */
		protected function determineDrawWinner () : Player
		{
			var cardsOne : int = this.game.playerOne.principalPack.cards.length + this.game.playerOne.aditionalPack.cards.length;
			var cardsTwo : int = this.game.playerTwo.principalPack.cards.length + this.game.playerTwo.aditionalPack.cards.length;
			
			if (cardsOne == 1) {
				return this.game.playerOne;
			} else if (cardsTwo == 1) {
				return this.game.playerTwo;
			} else {
				return null;
			}
			
		}
		
		/**
		 * Busca por cartas que tenham ficado no monte, para dá-las ao vencedor
		 */
		protected function getCardsDrawed (winner : Player) : void
		{
			for each (var card : Card in this.game.cardsDrawed) {
				winner.aditionalPack.addCard(card);
			}
			this.game.cardsDrawed.removeAll();
		}
		
		/**
		 * Coloca as cartas que ficaram empatadas no monte, para o próximo round
		 */
		protected function putCardsDrawed () : void
		{
			this.game.cardsDrawed.addItem(this.cardOne);
			this.game.cardsDrawed.addItem(this.cardTwo);
		}
		
		/**
		 * Finaliza o round
		 * @param winner
		 */
		public function finalize () : void
		{
			if (!_isFinalized && attributeName) {
				this.determineWinner(attributeName);
			}
		}
		
		/**
		 * Inicia um novo round
		 * @return round
		 */
		public function start () : void
		{
			
			if (!this._isFinalized) {
				
				cardOne = this.game.playerOne.principalPack.popCard();
				cardTwo = this.game.playerTwo.principalPack.popCard();
				
			}
			
		}
		
	}
}